package starfire.ftp;

import java.io.*;
import java.util.*;
import java.util.regex.*;

/**
 * @author tianshi
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class DefaultFtpParser
    implements FtpParser {

  private static DefaultFtpParser parser = new DefaultFtpParser();

  private DefaultFtpParser() {}

  public static DefaultFtpParser getParser() {
    return parser;
  }

  public FtpFile parse(String listline) {
    String attribute = null;
    String name = null;
    String createTime = null;
    boolean isDir = false;
    long length = 0;

    boolean find = false;
    //match unix ftp file
    Pattern unix = Pattern.compile(
        "(^[d|\\-]\\S+)\\s+\\d+\\s+\\w+\\s+\\w+\\s+(\\d+)\\s+" +
        "(\\w+\\s+\\w+\\s+\\S+)\\s+(.+)");
    Pattern windows = Pattern.compile(
        "(^[\\w\\-]+\\s+[\\w:]+)\\s+(\\S+)\\s+(.+)");
    Matcher match = unix.matcher(listline);
    if (match.find()) {
      attribute = match.group(1);
      if (attribute.toLowerCase().startsWith("d")) {
        isDir = true;
      }
      else {
        isDir = false;
      }
      length = Long.parseLong(match.group(2));
      createTime = match.group(3);
      name = match.group(4);
      find = true;
    }
    else {
      match = windows.matcher(listline);
      if (match.find()) {
        attribute = "read";
        createTime = match.group(1);
        String temp = match.group(2);
        if (temp.indexOf("<DIR>") != -1) {
          isDir = true;
        }
        else {
          isDir = false;
          try {
            length = Long.parseLong(temp);
          }
          catch (Exception e) {
            length = 0;
          }
        }
        name = match.group(3);
        find = true;
      }
    }
    if (find) {
      FtpFile file = new FtpFile(name);
      file.attribute = attribute;
      file.createTime = createTime;
      file.length = length;
      file.isDir = isDir;
      if (isDir) {
        file.children = new Vector(5);
      }
      return file;
    }
    else {
      return null;
    }
  }

  public Object[] parse(InputStream in) {
    String line = null;
    Vector v = new Vector(5);
    FtpFile f = null;
    if (in==null)
      return null;
    BufferedReader br = new BufferedReader(
        new InputStreamReader(in));
    int count = 0;
    while (true) {
      try {
        line = br.readLine();
        if (line == null) {
          break;
        }
        f = parser.parse(line);
        if (f == null) {
          continue;
        }
        if (f.getName().equals(".")
            || f.getName().equals("..")) {
          continue;
        }
        v.add(f);
      }
      catch (IOException e) {
        e.printStackTrace();
        return null;
      }
    }
    if (v.isEmpty()) {
      return null;
    }
    else {
      return v.toArray();
    }
  }

}
